﻿Imports System.Data.SqlClient

Public Class CThucDon

#Region "Attribute"
    Dim _iMaMonAn As Integer
    Dim _strTenMonAn As String
    Dim _strMaLoaiMonAn As String
    Dim _iDonGia As Integer
    Dim _strMaDonVi As String
    Shared da As DataProvider
#End Region
#Region "Property"
    Property MaMonAn As Integer
        Get
            Return _iMaMonAn
        End Get
        Set(ByVal value As Integer)
            _iMaMonAn = value
        End Set
    End Property
    Property TenMonAn As String
        Get
            Return _strTenMonAn
        End Get
        Set(ByVal value As String)
            _strTenMonAn = value
        End Set
    End Property
    Property MaLoaiMonAn As String
        Get
            Return _strMaLoaiMonAn
        End Get
        Set(ByVal value As String)
            _strMaLoaiMonAn = value
        End Set
    End Property
    Property DonGia As Integer
        Get
            Return _iDonGia
        End Get
        Set(ByVal value As Integer)
            _iDonGia = value
        End Set
    End Property
    Property MaDonVi As String
        Get
            Return _strMaDonVi
        End Get
        Set(ByVal value As String)
            _strMaDonVi = value
        End Set
    End Property
#End Region

    Public Function LoadDsThucDon() As DataTable
        Dim dt As DataTable = Nothing
        da = New DataProvider()
        da.OpenConnect()
        Dim sql As String = "select td.MaMonAn, td.TenMonAn, l.TenLoaiMonAn, dv.TenDV, td.DonGia from THUCDON td, LOAIMONAN l, DONVI dv"
        sql += " where td.MaLoaiMonAn = l.MaLoaiMonAn and td.MaDonVi = dv.MaDV"
        dt = da.ShowData(sql)
        da.CloseConnect()
        Return dt
    End Function
    Public Shared Function InsertThucDon(ByVal thucdon As CThucDon)
        Try
            da = New DataProvider()
            da.OpenConnect()
            Dim sql As String = "INSERT INTO THUCDON VALUES(@TenMH,@MaLoai,@DonVi,@Gia)"
            Dim cmd As New SqlCommand(sql, da.con)
            cmd.Parameters.AddWithValue("@TenMH", thucdon.TenMonAn)
            cmd.Parameters.AddWithValue("@Gia", thucdon.DonGia)
            cmd.Parameters.AddWithValue("@MaLoai", thucdon.MaLoaiMonAn)
            cmd.Parameters.AddWithValue("@DonVi", thucdon.MaDonVi)

            cmd.ExecuteNonQuery()
            Return 1
        Catch ex As Exception
            MsgBox(ex.Message, MsgBoxStyle.Information, "Thông báo")
        Finally
            da.CloseConnect()
        End Try
        Return 0
    End Function
    Public Shared Sub DeleteThucDon(ByVal masp As String)
        da = New DataProvider()
        da.OpenConnect()
        Dim sql As String = "DELETE FROM THUCDON WHERE MaMonAn = @MaSP"
        Dim cmd As New SqlCommand(sql, da.con)
        cmd.CommandText = sql
        cmd.Parameters.AddWithValue("@MaSP", masp)
        cmd.ExecuteNonQuery()
    End Sub
    Public Shared Sub UpdateThucDon(ByVal thucdon As CThucDon)
        Try
            da = New DataProvider()
            da.OpenConnect()
            Dim sql As String = "UPDATE THUCDON " + _
                                "SET TenMonAn = @TenMonAn, MaLoaiMonAn = @MaLoaiMonAn,MaDonVi = @MaDonVi,DonGia = @DonGia " + _
                                "WHERE MaMonAn = @MaMonAn"
            Dim cmd As New SqlCommand(sql, da.con)
            cmd.Parameters.AddWithValue("@MaMonAn", thucdon.MaMonAn)
            cmd.Parameters.AddWithValue("@TenMonAn", thucdon.TenMonAn)
            cmd.Parameters.AddWithValue("@MaLoaiMonAn", thucdon.MaLoaiMonAn)
            cmd.Parameters.AddWithValue("@MaDonVi", thucdon.MaDonVi)
            cmd.Parameters.AddWithValue("@DonGia", thucdon.DonGia)
            cmd.ExecuteNonQuery()
            MsgBox("Update Thành công", MsgBoxStyle.Information, "Thông báo")
        Catch ex As Exception
            MsgBox(ex.Message, MsgBoxStyle.Information, "Thông báo")
        Finally
            da.CloseConnect()
        End Try
    End Sub
    Public Shared Function GetData(ByVal sql As String) As Object
        da = New DataProvider
        
        Return da.GetData(sql)
    End Function
End Class
